using System;
using System.Text.RegularExpressions;
using YTS.Tools;

namespace Test.ConsoleProgram.Algorithm.Work
{
    [TestDescription("工作逻辑: 映射帮助")]
    public class Test_ShineUponHelper : AbsBaseTestItem
    {
        /// <summary>
        /// 映射数据集枚举
        /// </summary>
        public enum EShineUponType
        {
            /// <summary>
            /// 儿推商城
            /// </summary>
            None = 0,

            /// <summary>
            /// 儿推商城
            /// </summary>
            ErTuiMall = 1,

            /// <summary>
            /// 小天心智慧云
            /// </summary>
            XtxCloud = 2,
        }

        public override void OnTest()
        {
            Assert.TestExe(AddSignOrderNo, EShineUponType.None, "74853", "0T74853");
            Assert.TestExe(AddSignOrderNo, EShineUponType.ErTuiMall, (string)null, "1T");
            Assert.TestExe(AddSignOrderNo, EShineUponType.ErTuiMall, "74853", "1T74853");
            Assert.TestExe(AddSignOrderNo, EShineUponType.XtxCloud, "TTfeEF333", "2TTTfeEF333");

            bool equalsMethod(dynamic v1, dynamic v2)
            {
                return JsonHelper.ToJSONString(v1) == JsonHelper.ToJSONString(v2);
            };
            dynamic TestDelSignOrderNo(string OrderNo)
            {
                var type = DelSignOrderNo(OrderNo, out string restoreOrderNo);
                return new { type, rno = restoreOrderNo };
            }
            Assert.TestExe(TestDelSignOrderNo, "0T74853",
                equalsMethod: equalsMethod,
                new { type = EShineUponType.None, rno = "74853" });
            Assert.TestExe(TestDelSignOrderNo, "1T74853",
                equalsMethod: equalsMethod,
                new { type = EShineUponType.ErTuiMall, rno = "74853" });
            Assert.TestExe(TestDelSignOrderNo, "2TTTfeEF333",
                equalsMethod: equalsMethod,
                new { type = EShineUponType.XtxCloud, rno = "TTfeEF333" });
            Assert.TestExe(TestDelSignOrderNo, "ET8454355",
                equalsMethod: equalsMethod,
                new { type = EShineUponType.None, rno = "ET8454355" });
            Assert.TestExe(TestDelSignOrderNo, "1T",
                equalsMethod: equalsMethod,
                new { type = EShineUponType.ErTuiMall, rno = "" });
        }

        public string AddSignOrderNo(EShineUponType eType, string OrderNo)
        {
            OrderNo = OrderNo ?? string.Empty;
            int value = (int)eType;
            return $"{value}T{OrderNo}";
        }

        public EShineUponType DelSignOrderNo(string OrderNo, out string restoreOrderNo)
        {
            OrderNo = OrderNo ?? string.Empty;
            Regex re = new Regex(@"(\d+)T([^\n]*)");
            Match match = re.Match(OrderNo);
            if (!match.Success)
            {
                restoreOrderNo = OrderNo;
                return EShineUponType.None;
            }
            var value = match.Groups[1].Value.ToString();
            restoreOrderNo = match.Groups[2].Value.ToString();
            try
            {
                Enum.TryParse(value, out EShineUponType result);
                return result;
            }
            catch (Exception)
            {
                return EShineUponType.None;
            }
        }
    }
}
